﻿using Management.Auxiliary;
using Management.Core.ReportConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Management.Areas.Control.Controllers
{
    public class ReportConfigController : Controller
    {
        ReportConfigManager rcm = new ReportConfigManager();
        //
        // GET: /Control/ReportConfig/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add([Bind(Include = "AdminID, Config, Model")]ReportConfig _config)
        {
            _config.Created_at = DateTime.Now;
            _config.Updated_at = DateTime.Now;
            Response _resp = new Response();
            if(ModelState.IsValid)
            {
                if (rcm.IsContains(u => u.AdminID == _config.AdminID && u.Model == _config.Model))
                {
                    _resp.Code = 0;
                    _resp.Message = "您已保存了该模型下的报表配置";
                }
                _resp = rcm.Add(_config);
            }
            else
            {
                _resp.Code = -1;
                _resp.Message = "添加失败";
            }
            return Json(_resp);
        }

        [HttpDelete]
        public ActionResult Delete(int reportconfigID)
        {
            Response _resp = new Response();
            var rcm = new ReportConfigManager();
            _resp = rcm.Delete(u => u.ReportConfigID == reportconfigID);
            return Json(_resp);
        }

        [HttpPost]
        public ActionResult Modify(int adminid, string config, string model)
        {
            Response _resp = new Response();
            var _config = rcm.FindList(u => u.AdminID == adminid && u.Model == model).First();
            _config.Updated_at = DateTime.Now;
            _config.Config = config;
            if (TryUpdateModel(_config, new string[] {"AdminID", "Config", "Model", "Updated_at" }))
            {                
                _resp = rcm.Update(_config);
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = "模型不符合条件，更新失败";
            }
            return Json(_resp);
        }
	}
}